﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SD.LLBLGen.Pro.ORMSupportClasses;
using WHMS.HelperClasses;
using WMS.HelperClasses;

namespace WMS
{
    public partial class FrmGeneralMultiSelecteItem : Form
    {
        public FrmGeneralMultiSelecteItem()
        {
            InitializeComponent();
        }

        public List<string> SelectedItemCodes { get; set; }
        public List<int> SelectedItemIds { get; set; }
        public string SelectedEntityType { get; set; }
        private RelationPredicateBucket _filter;

        /// <summary>
        /// 
        /// </summary>
        /// <param name="entityType">Store or Item</param>
        public FrmGeneralMultiSelecteItem(string entityType)
        {
            InitializeComponent();
            SelectedEntityType = entityType;
        }

        /// <summary>
        /// Overloaded contructor for adding predicates
        /// </summary>
        /// <param name="entityType">Store or Item</param>
        public FrmGeneralMultiSelecteItem(string entityType, RelationPredicateBucket relationPredicateBucket)
        {
            InitializeComponent();
            SelectedEntityType = entityType;

            if (SelectedEntityType == "Shelf")
            {
                dataGridView1.Columns[NameColumn.Index].Visible = false;
                SearchByNameCB.Visible = false;
                SearchNameTB.Visible = false;
            }

            _filter = relationPredicateBucket;
        }

        public FrmGeneralMultiSelecteItem(int itemType)
        {
            InitializeComponent();
        }
        private void BtnSearchClick(object sender, EventArgs e)
        {
            var fields = new ResultsetFields(3);
            
            SortExpression sorter = new SortExpression();
            var filter = new RelationPredicateBucket();

            if (SelectedEntityType == "Item")
            {
                fields.DefineField(ItemFields.Id, 0, "Id");
                fields.DefineField(ItemFields.Code, 1, "Code");
                fields.DefineField(ItemFields.Name, 2, "Name"); 

                //filter.Relations.Add(ItemEntity.Relations.ItemTypeEntityUsingTypeId);
                //filter.Relations.Add(ItemEntity.Relations.UnitEntityUsingUnitId);
            }
            else if (SelectedEntityType == "Store")
            {

                fields.DefineField(StoreFields.Id, 0, "Id");
                fields.DefineField(StoreFields.Code, 1, "Code");
                fields.DefineField(StoreFields.Name, 2, "Name");

                //filter.Relations.Add(StoreEntity.Relations.ProjectEntityUsingProjectId);
                //filter.Relations.Add(StoreEntity.Relations.StoreShelfBalanceEntityUsingStoreId);
            }


            else if (SelectedEntityType == "Supplier")
            {

                fields.DefineField(SupplierFields.Id, 0, "Id");
                fields.DefineField(SupplierFields.Code, 1, "Code");
                fields.DefineField(SupplierFields.Name, 2, "Name");
            }

            else if (SelectedEntityType == "Contractor")
            {

                fields.DefineField(ContractorFields.Id, 0, "Id");
                fields.DefineField(ContractorFields.Code, 1, "Code");
                fields.DefineField(ContractorFields.Name, 2, "Name");
            }

            else if (SelectedEntityType == "Shelf")
            {
                fields.DefineField(ShelfFields.Id, 0, "Id");
                fields.DefineField(ShelfFields.Code, 1, "Code");
                fields.DefineField(ShelfFields.BarCode, 2, "Name"); // added extra just for not resulting exceptions
            }

            if (SearchByCodeCB.Checked)
            {
                if (SelectedEntityType == "Item")
                {
                    sorter = new SortExpression(ItemFields.Code | SortOperator.Ascending);
                }
                else if (SelectedEntityType == "Store")
                {
                    sorter = new SortExpression(StoreFields.Code | SortOperator.Ascending);
                }
                else if (SelectedEntityType == "Supplier")
                {
                    sorter = new SortExpression(SupplierFields.Code | SortOperator.Ascending);
                }
                else if (SelectedEntityType == "Contractor")
                {
                    sorter = new SortExpression(ContractorFields.Code | SortOperator.Ascending);
                }
                else if (SelectedEntityType == "Shelf")
                {
                    sorter = new SortExpression(ShelfFields.Code | SortOperator.Ascending);
                }
            }
            else if (SearchByNameCB.Checked)
            {
                if (SelectedEntityType == "Item")
                {
                    sorter = new SortExpression(ItemFields.Name | SortOperator.Ascending);
                }
                else if (SelectedEntityType == "Store")
                {
                    sorter = new SortExpression(StoreFields.Name | SortOperator.Ascending);
                }
                else if (SelectedEntityType == "Supplier")
                {
                    sorter = new SortExpression(SupplierFields.Name | SortOperator.Ascending);
                }
                else if (SelectedEntityType == "Contractor")
                {
                    sorter = new SortExpression(ContractorFields.Name | SortOperator.Ascending);
                }
                else if (SelectedEntityType == "Shelf")
                {
                    sorter = new SortExpression(ShelfFields.BarCode | SortOperator.Ascending);
                }
            }

            if (SearchByCodeCB.Checked && !string.IsNullOrEmpty(SearchCodeTB.Text))
            {
                var codeTerm = SearchCodeTB.Text.Trim() + "%";

                if (SelectedEntityType == "Item")
                {
                    filter.PredicateExpression.Add(ItemFields.Code % codeTerm);


                }
                else if (SelectedEntityType == "Store")
                {
                    filter.PredicateExpression.Add(StoreFields.Code % codeTerm);


                }
                else if (SelectedEntityType == "Supplier")
                {
                    filter.PredicateExpression.Add(SupplierFields.Code % codeTerm);
                }
                else if (SelectedEntityType == "Contractor")
                {
                    filter.PredicateExpression.Add(ContractorFields.Code % codeTerm);
                }
                else if (SelectedEntityType == "Shelf")
                {
                    _filter.PredicateExpression.Add(ShelfFields.Code % codeTerm);
                }
            }

            if (SearchByNameCB.Checked && !string.IsNullOrEmpty(SearchNameTB.Text))
            {
                var nameTerm = SearchNameTB.Text.Trim() + "%";

                if (SelectedEntityType == "Item")
                {
                    filter.PredicateExpression.Add(ItemFields.Name % nameTerm);


                }
                else if (SelectedEntityType == "Store")
                {

                    filter.PredicateExpression.Add(StoreFields.Name % nameTerm);


                }
                else if (SelectedEntityType == "Supplier")
                {

                    filter.PredicateExpression.Add(SupplierFields.Name % nameTerm);


                }
                else if (SelectedEntityType == "Contractor")
                {

                    filter.PredicateExpression.Add(ContractorFields.Name % nameTerm);
                }
            }

            if (_filter != null)
                filter = _filter;

            var dt = DALHelper.GetDynamicList(fields, filter, sorter);
            dataGridView1.DataSource = dt;

            if (dt.Rows.Count > 0)
            {
                //dataGridView1.Rows[0].Selected = true;
                btnSelectAll.Enabled = true;
                btnRemoveSelection.Enabled = true;
            }
            else
            {
                btnSelectAll.Enabled = false;
                btnRemoveSelection.Enabled = false;
            }
        }

        private void BtnSelectClick(object sender, EventArgs e)
        {
            SelectedItemCodes=new List<string>();
            SelectedItemIds = new List<int>();

            foreach(DataGridViewRow row in dataGridView1.Rows)
            {
                if(row.Cells[SelectionColumn.Index].Value!=null && bool.Parse(row.Cells[SelectionColumn.Index].Value.ToString()))
                {
                    SelectedItemCodes.Add(row.Cells[2].Value.ToString());
                    SelectedItemIds.Add(int.Parse(row.Cells[1].Value.ToString()));
                }
            }

            //if (dataGridView1.SelectedRows.Count > 0)
            //{
            //    var name = (string)dataGridView1.SelectedRows[0].Cells[2].Value;
            //    var code = (string)dataGridView1.SelectedRows[0].Cells[1].Value;

            //    SelectedItemCode = code ;
            //    SelectedItemId = (int)dataGridView1.SelectedRows[0].Cells[0].Value;
            //}
            this.DialogResult = DialogResult.OK;

            this.Close();
        }

        private void SearchByCodeCbCheckedChanged(object sender, EventArgs e)
        {
            SearchCodeTB.Enabled = SearchByCodeCB.Checked;
        }

        private void SearchByNameCbCheckedChanged(object sender, EventArgs e)
        {
            SearchNameTB.Enabled = SearchByNameCB.Checked;
        }

        private void BtnSelectAllClick(object sender, EventArgs e)
        {
            foreach(DataGridViewRow row in dataGridView1.Rows)
            {
                row.Cells[SelectionColumn.Index].Value = true;
            }
        }

        private void BtnRemoveSelectionClick(object sender, EventArgs e)
        {
            foreach (DataGridViewRow row in dataGridView1.Rows)
            {
                row.Cells[SelectionColumn.Index].Value = false;
            }
        }
    }
}
